import * as THREE from "./threejs/three.module.js";
import * as ThreeComm from '../js/three-comm.js';
//1、周二 11:40  2、周三晚上不答疑 12：05  6：04

const DRAG_MESS_TYPES = ',BoxGeometry,BufferGeometry,';
const DRAG_MARKER_TYPES = ',SphereGeometry,';

//判断子字符串是否在当前字符串中
export function isContain(str,sub){
  if(null==str || typeof(str)=='undefined' || ''==str){
    return false;
  }
  if(null==sub || typeof(sub)=='undefined' || ''==sub){
    return false;
  }
  var sSub = ','+sub+',';
  var sStr = str;
  if(!sStr.startsWith(',')){
    sStr = ','+sStr;
  }
  if(!sStr.endsWith(',')){
    sStr += ',';
  }
  if(sStr.indexOf(sSub)<0){
    return false;
  }
  return true;
}
//判断当前模型是否为拖动标志物
export function isDragMarker(hMarker){
  var messType = ThreeComm.getMessType(hMarker);
  var bIsMarker= isContain(DRAG_MARKER_TYPES,messType);
  var dataType = ThreeComm.getMessUserDataType(hMarker);
  if('drag'!=dataType){
    return false;
  }
  return bIsMarker;
}
//判断当前模型是否为拖动目标
export function isDragMess(hMess){
  var messType = ThreeComm.getMessType(hMess);
  var bIsDrag  = isContain(DRAG_MESS_TYPES,messType);
  var dataType = ThreeComm.getMessUserDataType(hMess);
  if('drag'==dataType || ''==dataType){
    return false;
  }
  return bIsDrag;
}
//创建选择的模型的各个面的中心点的拖动图标
export function createDragMarkers(hScene,hMess){
  if(ThreeComm.isEmpty(hScene) || ThreeComm.isEmpty(hMess)){
    return;
  }
  var messIdex = ThreeComm.getMessGeometryIndex(hMess);
  if(null==messIdex || typeof(messIdex)=="undefined"){
    return null;
  }
  var vertexs  = ThreeComm.getMessVertexs(hMess);
  if(null==vertexs || typeof(vertexs)=='undefined'){
    return null;
  }
  var idxArr = messIdex.array;
  if(ThreeComm.isEmpty(idxArr) || !(idxArr  instanceof Uint16Array)){
    return;
  }
  var iSize = idxArr.length;
  var faceSize = ThreeComm.accDiv(iSize,6);
  var facePoint;
  var dargMarker;
  cleanDragMarkers();
  for(var faceIdx=0;faceIdx<faceSize;faceIdx++){
    facePoint = getCenterByFaceIdx(vertexs,idxArr,faceIdx);
    dargMarker = createDragMarker(hScene,faceIdx,facePoint);
    hMess.add(dargMarker);
  }
}

//创建选择的模型的各个面的中心点的拖动图标
var dargMarkers = []
const dargGeometry = new THREE.SphereGeometry( 3, 32, 16 );
const dargMaterial = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
//清空拖拉模型的分组或者是父亲
export function cleanDragMarkers(){
  var iSize = dargMarkers.length;
  var dargMarker;
  var hParent,messType;
  for(var i=0;i<iSize;i++){
    dargMarker = dargMarkers[i];
    if(null==dargMarker || typeof(dargMarker)=='undefined'){
      continue;
    }
    hParent = dargMarker.parent;
    messType = ThreeComm.getMessType(hParent);
    if(ThreeComm.isEmpty(messType) || 'Scene'==messType){
      continue;
    }
    dargMarker.removeFromParent();
  }
}
export function createDragMarker(hScene,faceIdx,facePoint){
  if(ThreeComm.isEmpty(hScene) || ThreeComm.isEmpty(facePoint) || !ThreeComm.isNum(faceIdx)){
    return null;
  }
  var dragMarker;
  while(faceIdx>=dargMarkers.length){
    dragMarker =  new THREE.Mesh( dargGeometry, dargMaterial )
    dragMarker.visible = false;
    hScene.add(dragMarker);
    dargMarkers.push(dragMarker)
  }
  var center = facePoint.center;
  dragMarker = dargMarkers[faceIdx]
  dragMarker.visible = true;
  dragMarker.position.x = center.x;
  dragMarker.position.y = center.y;
  dragMarker.position.z = center.z;

  var points = center.points;

  dragMarker.userData = {faceIdx:faceIdx,type:'drag',points:points}
  return dragMarker;
}

export function getCenterByFaceIdx(vertexs,idxArr,faceIdx){
  if(!(idxArr  instanceof Uint16Array) || !(vertexs  instanceof Float32Array) || !ThreeComm.isNum(faceIdx)){
    console.log('getCenterByFaceIdx is null')
    return null;
  }
  var idx = ThreeComm.accMul(faceIdx,6);
  var iSizeIdx     = idxArr.length;
  var iSizeVertexs = vertexs.length;
  if(iSizeIdx-1>idx+6){
    iSizeIdx = idx+6;
  }
  var fIdx,vIdx;
  var x,y,z,sx=0,sy=0,sz=0,sIdx=0;
  var tmp = []
  var bIsExist ;
  for(var i= idx;i<iSizeIdx;i++){
    fIdx = idxArr[i];
    bIsExist = false;
    for(var j=0;j<tmp.length;j++){
      if(fIdx==tmp[j]){
        bIsExist = true;
        break;
      }
    }
    if(!bIsExist){
      tmp.push(fIdx);
    }
  }
  var points = [];
  iSizeIdx = tmp.length;
  for(var i= 0;i<iSizeIdx;i++){
    fIdx = tmp[i];
    vIdx = fIdx*3;
    if(vIdx+3>iSizeVertexs){
      continue;
    }
    x = vertexs[vIdx];
    y = vertexs[vIdx+1];
    z = vertexs[vIdx+2];
    sx = ThreeComm.accAdd(sx,x);
    sy = ThreeComm.accAdd(sy,y);
    sz = ThreeComm.accAdd(sz,z);
    sIdx ++;
    points.push({x:x,y:y,z:z});
  }
  if(sIdx<1){
    return null;
  }
  sx = ThreeComm.accDiv(sx,sIdx);
  sy = ThreeComm.accDiv(sy,sIdx);
  sz = ThreeComm.accDiv(sz,sIdx);
  var result ={center:{x:sx,y:sy,z:sz},points:points} ;
  return result;
}